package service

import (
	"bytes"
	"encoding/json"
	"encoding/xml"
	"errors"
	"fmt"
	"git.inedcn.com/ined/cmn"
	"git.inedcn.com/ined/cmn/dotenv"
	"io/ioutil"
	"net/http"
	constant "rongyy/src/constants"
	"rongyy/src/constants/MesCode"
	"rongyy/src/constants/TradeCode"
	"rongyy/src/helper"
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"runtime"
	"strconv"
	"strings"
	"time"
)

type HisService struct {
}

// FindPatientCard
//
//	@Description: 查询卡
//	@receiver pc
//	@param req
//	@return *models.Resp3013
//	@return error
func (pc *HisService) FindPatientCard(req *models.Req3013Params) (*models.Resp3013, error) {
	cardTypeCode := "SCEHC"
	if req.CardTypeCode != "" {
		cardTypeCode = req.CardTypeCode
	}
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.FindPatientCard).
		Xml("TradeCode", TradeCode.FindPatientCard).
		Xml("IDType", req.IDType).
		Xml("IDNo", req.IDNo).
		Xml("PatientName", req.PatientName).
		Xml("CardTypeCode", cardTypeCode).
		Xml("PatientCard", req.PatientCard).
		Xml("MobilePhone", "").
		Xml("UserID", constant.UserID).
		Xml("TerminalID", "").
		Xml("PatientSex", "").
		Xml("PatientDob", "").
		Xml("HospitalId", constant.HospitalId).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp3013 := &models.Resp3013{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp3013)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}

	return resp3013, nil
}

// SavePatientCard
//
//	@Description: 发卡
//	@receiver pc
//	@param req
//	@return *models.Resp3014
//	@return error
func (pc *HisService) SavePatientCard(req *models.Req3014Params) (*models.Resp3014, error) {
	cardTypeCode := constant.CardTypeCode
	if req.CardTypeCode != "" {
		cardTypeCode = req.CardTypeCode
	}
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.SavePatientCard).
		Xml("TradeCode", TradeCode.SavePatientCard).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserID", constant.UserID).
		Xml("PayModeCode", constant.PayModeCode).
		Xml("PatientType", constant.PatientType).
		Xml("CardTypeCode", cardTypeCode).
		Xml("PatientCard", req.PatientCard).
		Xml("IDType", req.IDType).
		Xml("IDNo", req.IDNo).
		Xml("PatientName", req.PatientName).
		Xml("Sex", req.Sex).
		Xml("DOB", req.DOB).
		Xml("Mobile", req.Mobile).
		Xml("MaritalStatus", req.MaritalStatus).
		Xml("Address", req.Address).
		Xml("ContactName", req.ContactName).
		Xml("ContactTelNo", req.ContactTelNo).
		Xml("ContactIDNo", req.ContactIDNo).
		Xml("Nation", req.Nation).
		Xml("Occupation", req.Occupation).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp3014 := &models.Resp3014{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp3014)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3014 failed ", err)
		return nil, err
	}

	return resp3014, nil
}

func (rg *HisService) GetPatInfo(req *models.Req3300Params) (*models.Resp3300, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetPatInfo).
		Xml("TradeCode", TradeCode.GetPatInfo).
		Xml("HospitalId", constant.HospitalId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("CardType", req.CardType).
		Xml("PatientID", req.PatientId).
		Xml("PatientName", req.PatientName).
		Xml("IDCardType", req.IDCardType).
		Xml("IDNo", req.IDNo).
		Xml("Phone", req.Phone).
		EndXml()

	//middlewares.Logger.Println("GetPatInfo xml ", requestXml)

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	//middlewares.Logger.Println("GetPatInfo result ", result.Body.MiResp.MiResult)

	resp3300 := &models.Resp3300{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp3300)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3300 failed ", err)
		return nil, err
	}
	if resp3300.ResultCode != "0" {
		return nil, errors.New(resp3300.ResultContent)
	}

	middlewares.Logger.Println("GetPatInfo resp3300 ", resp3300)

	return resp3300, nil
}

func (pc *HisService) UpdatePatInfo(req *models.Req3016Params) (*models.Resp3016, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.UpdatePatInfo).
		Xml("TradeCode", TradeCode.UpdatePatInfo).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("PatientID", req.PatientId).
		Xml("CardTypeCode", req.CardTypeCode).
		Xml("PatientCard", req.PatientCard).
		Xml("IDType", "01").
		Xml("IDNo", req.IdNo).
		Xml("CardTypeCode", req.CardTypeCode).
		Xml("PatName", req.PatientName).
		Xml("PatSex", req.Sex).
		Xml("PatBirth", req.PatBirth).
		Xml("HospitalId", constant.HospitalId).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp3016 := &models.Resp3016{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp3016)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3016 failed ", err)
		return nil, err
	}

	return resp3016, nil
}

func (pc *HisService) GetOccupations() (*models.RespOccupation, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetOccupations).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	respOccupation := &models.RespOccupation{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), respOccupation)
	if err != nil {
		middlewares.Logger.Println("Unmarshal respOccupation failed ", err)
		return nil, err
	}

	return respOccupation, nil
}

func (pc *HisService) GetHisBills(req *models.Req6001Params) (*models.Resp6001, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetHisBills).
		Xml("TradeCode", TradeCode.GetHisBills).
		Xml("HospitalID", req.HospitalId).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("TradeChannel", req.TradeChannel).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp6001 := &models.Resp6001{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp6001)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp6001 failed ", err)
		return nil, err
	}

	return resp6001, nil
}

func (pc *HisService) GetReportIdsByAdm(admNo string) (*models.Resp0036, error) {
	var requestXml helper.XmlString
	_ = *requestXml.ReportBeginXml(MesCode.GetReportIdsByAdm).ReportXml(admNo).ReportEndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp0036 := &models.Resp0036{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp0036)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp0036 failed ", err)
		return nil, err
	}

	return resp0036, nil
}

func (pc *HisService) GetReportsById(reportId string) (*models.Resp0037, error) {
	var requestXml helper.XmlString
	_ = *requestXml.ReportBeginXml(MesCode.GetReportsById).ReportXml(reportId).ReportEndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp0037 := &models.Resp0037{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp0037)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp6001 failed ", err)
		return nil, err
	}

	return resp0037, nil
}

func (op *HisService) GetAdmByCardNo(req *models.Req4902Params) (*models.Resp4902, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetAdmByCardNo).
		Xml("TradeCode", TradeCode.GetAdmByCardNo).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PatientID", req.PatientId).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4902 := &models.Resp4902{}

	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4902)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4902 failed ", err)
		return nil, err
	}
	return resp4902, nil
}

func (op *HisService) GetMenteAdm(req *models.Req0050Params) (*models.Resp4902, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetMenteAdm).
		Xml("TradeCode", TradeCode.GetMenteAdm).
		Xml("HospitalID", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PatientID", req.PatientId).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4902 := &models.Resp4902{}

	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4902)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4902 failed ", err)
		return nil, err
	}
	return resp4902, nil
}

func (op *HisService) GetChargeOrder(req *models.Req4904Params) (*models.Resp4904, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetChargeOrder).
		Xml("TradeCode", TradeCode.GetChargeOrder).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PatientID", req.PatientId).
		Xml("Adm", req.Adm).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4904 := &models.Resp4904{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4904)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4904 failed ", err)
		return nil, err
	}

	return resp4904, nil
}

func (op *HisService) PreBillCharge(req *models.Req4905Params) (*models.Resp4905, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.PreBillCharge).
		Xml("TradeCode", TradeCode.PreBillCharge).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PayModeCode", constant.PayModeCode).
		Xml("PatientID", req.PatientId).
		Xml("OrderNo", req.OrderNo).
		Xml("OrderSum", req.OrderSum).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4905 := &models.Resp4905{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4905)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4905 failed ", err)
		return nil, err
	}

	return resp4905, nil
}

func (op *HisService) CompleteCharge(req *models.Req4906Params) (*models.Resp4906, error) {
	if req.PayModeCode == "" {
		req.PayModeCode = constant.PayModeCode
	}
	var requestXml, payDetailsXml helper.XmlString
	_ = *payDetailsXml.Xml("PayModeCode", req.PayModeCode).
		Xml("PayAmt", req.PayAmt).
		Xml("PlatformNo", req.PlatformNo).
		Xml("OutPayNo", req.OutPayNo).
		Xml("PayDate", req.PayDate).
		Xml("PayTime", req.PayTime)
	_ = *requestXml.BeginXml(MesCode.CompleteCharge).
		Xml("TradeCode", TradeCode.CompleteCharge).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PayModeCode", req.PayModeCode).
		Xml("PatientID", req.PatientId).
		Xml("InvoiceNoStr", req.InvoiceNoStr).
		Xml("OrderNo", req.OrderNo).
		Xml("PayDetails", string(payDetailsXml)).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4906 := &models.Resp4906{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4906)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4906 failed ", err)
		return nil, err
	}

	return resp4906, nil
}

func (op *HisService) GetPaidRecord(req *models.Req4907Params) (*models.Resp4907, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetPaidRecord).
		Xml("TradeCode", TradeCode.GetPaidRecord).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PayModeCode", constant.PayModeCode).
		Xml("PatientID", req.PatientId).
		Xml("Adm", req.Adm).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	resp4907 := &models.Resp4907{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4907)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4907 failed ", err)
		return nil, err
	}

	return resp4907, nil
}

func (op *HisService) GetPaidRecordDetails(req *models.Req4908Params) (*models.Resp4908, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.GetPaidRecordDetails).
		Xml("TradeCode", TradeCode.GetPaidRecordDetails).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PayModeCode", constant.PayModeCode).
		Xml("PatientID", req.PatientId).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("Adm", req.Adm).
		Xml("OrderNo", req.OrderNo).
		Xml("InvoiceNo", req.InvoiceNo).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4908 := &models.Resp4908{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4908)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4908 failed ", err)
		return nil, err
	}

	return resp4908, nil
}

func (op *HisService) CancelCharge(req *models.Req4910Params) (*models.Resp4910, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.CancelCharge).
		Xml("TradeCode", TradeCode.CancelCharge).
		Xml("HospitalId", constant.HospitalId).
		Xml("UserCode", constant.UserCode).
		Xml("PatientID", req.PatientId).
		Xml("InvoiceNoStr", req.InvoiceNoStr).
		Xml("OrderNo", req.OrderNo).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp4910 := &models.Resp4910{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp4910)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp4910 failed ", err)
		return nil, err
	}

	return resp4910, nil
}

func (op *HisService) Refund(req *models.ReqOpRefundParams) (interface{}, error) {
	url := "https://testwxmp.zhongkangcloud.com/api/outpatientPayment/refundCallback"
	contentType := "application/soap+xml;charset=UTF-8"
	data := make(map[string]interface{})
	data["order_sn"] = req.OrderSn
	data["fee"] = req.Fee
	bytesData, _ := json.Marshal(data)
	resp, err := http.Post(url, contentType, bytes.NewReader(bytesData))
	if err != nil {
		middlewares.Logger.Println("Refund Post failed ", err)
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		middlewares.Logger.Println("read response body failed", err)
		return nil, err
	}
	type RefundResp struct {
		Success bool        `json:"success"`
		Data    interface{} `json:"data"`
		Errors  string      `json:"errors"`
	}

	rr := &RefundResp{}
	err = json.Unmarshal(body, rr)
	middlewares.Logger.Println(string(body))
	middlewares.Logger.Println("sadsdasdasdadsaads")
	if err != nil {
		middlewares.Logger.Println("Unmarshal refundResp failed ", err)
		return nil, err
	}
	fmt.Println(rr)
	if !rr.Success {
		middlewares.Logger.Println("refundResp error ", rr.Errors)
		return nil, errors.New(rr.Errors)
	}

	return rr.Data, nil
}

// 二级科室列表（1012）

func (rg *HisService) QueryDepartment() (*models.Resp1012, error) {
	local, _ := time.LoadLocation("Asia/Shanghai")
	fmt.Println(time.Now().In(local).Format("2006-01-01"))
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.QueryDepartment).
		Xml("TradeCode", TradeCode.QueryDepartment).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("DepartmentCode", "").
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		//Xml("StartDate", time.Now().In(local).Format("2006-01-01")).
		//Xml("EndDate",time.Now().In(local).Format("2006-01-01")).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp1012 := &models.Resp1012{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp1012)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1012 failed ", err)
		return nil, err
	}
	if resp1012.ResultCode != "0" {
		return nil, errors.New(resp1012.ResultContent)
	}

	return resp1012, nil
}

func (rg *HisService) QueryDoctor(dptCode string) (interface{}, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.QueryDoctor).
		Xml("TradeCode", TradeCode.QueryDoctor).
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("DepartmentCode", dptCode).
		Xml("ExtUserID", constant.ExtUserID).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp1013 := models.Resp1013{}

	//data := `<Response><ResultCode>0</ResultCode><RecordCount>2</RecordCount><Doctors><Doctor><DoctorCode>2</DoctorCode><DoctorName>曾义岚</DoctorName><DoctotLevelCode>主任医师</DoctotLevelCode><DoctorLevel>1</DoctorLevel><DeptId>128</DeptId><DeptName>肝病中心门诊</DeptName></Doctor><Doctor><DoctorCode>5</DoctorCode><DoctorName>郑永利</DoctorName><DoctotLevelCode>副主任医师</DoctotLevelCode><DoctorLevel>2</DoctorLevel><DeptId>128</DeptId><DeptName>肝病中心门诊</DeptName></Doctor></Doctors></Response>`

	//  data := `<Response>
	//	<ResultCode>dgdg</ResultCode>
	//	<ResultContent>dgsgh</ResultContent>
	//	<RecordCount>dgfdgf</RecordCount>
	//	<Doctors>
	//		<Doctor>
	//			<DoctorCode>sdgdsg</DoctorCode>
	//			<DoctorName>sdgsdg</DoctorName>
	//			<DoctorLevelCode>gdsfgdsf</DoctorLevelCode>
	//			<DoctorLevel>gsdgfg</DoctorLevel>
	//			<DeptId>gsddsfg</DeptId>
	//			<DeptName>sdgfdfsg</DeptName>
	//			<Description>sdgdsfg</Description>
	//		</Doctor>
	//		<Doctor>
	//			<DoctorCode>sdgdsgf</DoctorCode>
	//			<DoctorName>dgsfdsfg</DoctorName>
	//			<DoctorLevelCode>dgdgf</DoctorLevelCode>
	//			<DoctorLevel>gddfg</DoctorLevel>
	//			<DeptId>dfsgdsgf</DeptId>
	//			<DeptName>dgsfdsfg</DeptName>
	//			<Description>gddgf</Description>
	//		</Doctor>
	//	</Doctors>
	//</Response>
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), &resp1013)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1013 failed ", err)
		return nil, err
	}

	return resp1013, nil
}

func (rg *HisService) QueryAdmSchedule(req *models.Req1004Params) ([]models.Schedule, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.QueryAdmSchedule).
		Xml("TradeCode", TradeCode.QueryAdmSchedule).
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("DepartmentCode", req.DepartmentCode).
		Xml("ServiceCode", "").
		Xml("DoctorCode", "").
		Xml("DoctorName", req.DoctorName).
		Xml("RBASSessionCode", "").
		Xml("StopScheduleFlag", "").
		Xml("BillTypeID", "").
		Xml("SessType", "").
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp1004 := &models.Resp1004{}

	//  data := `<Response>
	//    <ResultCode>0</ResultCode>
	//    <RecordCount>5</RecordCount>
	//    <Schedules>
	//        <Schedule>
	//            <ScheduleItemCode>1338||3714</ScheduleItemCode>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>01</SessionCode>
	//            <SessionName>上午</SessionName>
	//            <StartTime>08:15</StartTime>
	//            <EndTime>12:00</EndTime>
	//            <DepartmentCode>128</DepartmentCode>
	//            <DepartmentName>肝病中心门诊</DepartmentName>
	//            <DoctorCode>928</DoctorCode>
	//            <DoctorName>肝病中心普通门诊</DoctorName>
	//            <DoctorTitleCode>8</DoctorTitleCode>
	//            <DoctorTitle>主治医师</DoctorTitle>
	//            <DoctorSessTypeCode>8</DoctorSessTypeCode>
	//            <DoctorSessType>主治医师</DoctorSessType>
	//            <Fee>11</Fee>
	//            <RegFee>0</RegFee>
	//            <CheckupFee>11</CheckupFee>
	//            <OtherFee>0</OtherFee>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200</AvailableNumStr>
	//            <AdmitAddress>二楼</AdmitAddress>
	//            <Note>20@%Collection.ListOfDT</Note>
	//            <ScheduleStatus>1</ScheduleStatus>
	//            <AvailableTotalNum>200</AvailableTotalNum>
	//            <AvailableLeftNum>200</AvailableLeftNum>
	//            <TimeRangeFlag>1</TimeRangeFlag>
	//        </Schedule>
	//        <Schedule>
	//            <ScheduleItemCode>2196||3468</ScheduleItemCode>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>01</SessionCode>
	//            <SessionName>上午</SessionName>
	//            <StartTime>08:15</StartTime>
	//            <EndTime>12:00</EndTime>
	//            <DepartmentCode>128</DepartmentCode>
	//            <DepartmentName>肝病中心门诊</DepartmentName>
	//            <DoctorCode>954</DoctorCode>
	//            <DoctorName>肝病中心门诊免费号</DoctorName>
	//            <DoctorTitleCode>38</DoctorTitleCode>
	//            <DoctorTitle>免费号</DoctorTitle>
	//            <DoctorSessTypeCode>38</DoctorSessTypeCode>
	//            <DoctorSessType>免费号</DoctorSessType>
	//            <Fee>0</Fee>
	//            <RegFee>0</RegFee>
	//            <CheckupFee>0</CheckupFee>
	//            <OtherFee>0</OtherFee>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50</AvailableNumStr>
	//            <AdmitAddress>二楼</AdmitAddress>
	//            <Note>23@%Collection.ListOfDT</Note>
	//            <ScheduleStatus>1</ScheduleStatus>
	//            <AvailableTotalNum>50</AvailableTotalNum>
	//            <AvailableLeftNum>50</AvailableLeftNum>
	//            <TimeRangeFlag>0</TimeRangeFlag>
	//        </Schedule>
	//        <Schedule>
	//            <ScheduleItemCode>3||1476</ScheduleItemCode>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>01</SessionCode>
	//            <SessionName>上午</SessionName>
	//            <StartTime>08:15</StartTime>
	//            <EndTime>12:00</EndTime>
	//            <DepartmentCode>128</DepartmentCode>
	//            <DepartmentName>肝病中心门诊</DepartmentName>
	//            <DoctorCode>2</DoctorCode>
	//            <DoctorName>曾义岚</DoctorName>
	//            <DoctorTitleCode>22</DoctorTitleCode>
	//            <DoctorTitle>主任医师</DoctorTitle>
	//            <DoctorSessTypeCode>22</DoctorSessTypeCode>
	//            <DoctorSessType>主任医师</DoctorSessType>
	//            <Fee>15</Fee>
	//            <RegFee>0</RegFee>
	//            <CheckupFee>15</CheckupFee>
	//            <OtherFee>0</OtherFee>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120</AvailableNumStr>
	//            <Note>25@%Collection.ListOfDT</Note>
	//            <ScheduleStatus>1</ScheduleStatus>
	//            <AvailableTotalNum>120</AvailableTotalNum>
	//            <AvailableLeftNum>120</AvailableLeftNum>
	//            <TimeRangeFlag>1</TimeRangeFlag>
	//        </Schedule>
	//        <Schedule>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>14:00</StartTime>
	//            <EndTime>17:30</EndTime>
	//            <DepartmentCode>128</DepartmentCode>
	//            <DepartmentName>肝病中心门诊</DepartmentName>
	//            <DoctorCode>928</DoctorCode>
	//            <DoctorName>肝病中心普通门诊</DoctorName>
	//            <DoctorTitleCode>8</DoctorTitleCode>
	//            <DoctorTitle>主治医师</DoctorTitle>
	//            <DoctorSessTypeCode>8</DoctorSessTypeCode>
	//            <DoctorSessType>主治医师</DoctorSessType>
	//            <Fee>11</Fee>
	//            <RegFee>0</RegFee>
	//            <CheckupFee>11</CheckupFee>
	//            <OtherFee>0</OtherFee>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200</AvailableNumStr>
	//            <AdmitAddress>二楼</AdmitAddress>
	//            <Note>27@%Collection.ListOfDT</Note>
	//            <ScheduleStatus>1</ScheduleStatus>
	//            <AvailableTotalNum>200</AvailableTotalNum>
	//            <AvailableLeftNum>200</AvailableLeftNum>
	//            <TimeRangeFlag>1</TimeRangeFlag>
	//        </Schedule>
	//        <Schedule>
	//            <ScheduleItemCode>2196||3479</ScheduleItemCode>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>14:00</StartTime>
	//            <EndTime>17:30</EndTime>
	//            <DepartmentCode>128</DepartmentCode>
	//            <DepartmentName>肝病中心门诊</DepartmentName>
	//            <DoctorCode>954</DoctorCode>
	//            <DoctorName>肝病中心门诊免费号</DoctorName>
	//            <DoctorTitleCode>38</DoctorTitleCode>
	//            <DoctorTitle>免费号</DoctorTitle>
	//            <DoctorSessTypeCode>38</DoctorSessTypeCode>
	//            <DoctorSessType>免费号</DoctorSessType>
	//            <Fee>0</Fee>
	//            <RegFee>0</RegFee>
	//            <CheckupFee>0</CheckupFee>
	//            <OtherFee>0</OtherFee>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50</AvailableNumStr>
	//            <AdmitAddress>二楼</AdmitAddress>
	//            <Note>29@%Collection.ListOfDT</Note>
	//            <ScheduleStatus>1</ScheduleStatus>
	//            <AvailableTotalNum>50</AvailableTotalNum>
	//            <AvailableLeftNum>50</AvailableLeftNum>
	//            <TimeRangeFlag>0</TimeRangeFlag>
	//        </Schedule>
	//    </Schedules>
	//</Response>
	//
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp1004)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1004 failed ", err)
		return nil, err
	}

	if resp1004.ResultCode != "0" {
		return nil, errors.New(resp1004.ResultContent)
	}

	return resp1004.Schedules, nil
}

func (rg *HisService) QueryScheduleTimeInfo(scheduleItemCode string) ([]models.TimeRange, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.QueryScheduleTimeInfo).
		Xml("TradeCode", TradeCode.QueryScheduleTimeInfo).
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("DepartmentCode", "").
		Xml("DoctorCode", "").
		Xml("RBASSessionCode", "").
		Xml("ScheduleItemCode", scheduleItemCode).
		Xml("ServiceDate", "").
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp10041 := models.Resp10041{}

	//  data := `<Response>
	//    <ResultCode>0</ResultCode>
	//    <RecordCount>7</RecordCount>
	//    <TimeRanges>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>1</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>14:00</StartTime>
	//            <EndTime>14:30</EndTime>
	//            <AvailableNumStr>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29</AvailableNumStr>
	//            <AvailableTotalNum>29</AvailableTotalNum>
	//            <AvailableLeftNum>29</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>2</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>14:30</StartTime>
	//            <EndTime>15:00</EndTime>
	//            <AvailableNumStr>30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58</AvailableNumStr>
	//            <AvailableTotalNum>29</AvailableTotalNum>
	//            <AvailableLeftNum>29</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>3</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>15:00</StartTime>
	//            <EndTime>15:30</EndTime>
	//            <AvailableNumStr>59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87</AvailableNumStr>
	//            <AvailableTotalNum>29</AvailableTotalNum>
	//            <AvailableLeftNum>29</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>4</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>15:30</StartTime>
	//            <EndTime>16:00</EndTime>
	//            <AvailableNumStr>88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116</AvailableNumStr>
	//            <AvailableTotalNum>29</AvailableTotalNum>
	//            <AvailableLeftNum>29</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>5</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>16:00</StartTime>
	//            <EndTime>16:30</EndTime>
	//            <AvailableNumStr>117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144</AvailableNumStr>
	//            <AvailableTotalNum>28</AvailableTotalNum>
	//            <AvailableLeftNum>28</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>6</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>16:30</StartTime>
	//            <EndTime>17:00</EndTime>
	//            <AvailableNumStr>145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172</AvailableNumStr>
	//            <AvailableTotalNum>28</AvailableTotalNum>
	//            <AvailableLeftNum>28</AvailableLeftNum>
	//        </TimeRange>
	//        <TimeRange>
	//            <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//            <TimeRangeSeqNo>7</TimeRangeSeqNo>
	//            <ServiceDate>2020-07-28</ServiceDate>
	//            <WeekDay>2</WeekDay>
	//            <SessionCode>02</SessionCode>
	//            <SessionName>下午</SessionName>
	//            <StartTime>17:00</StartTime>
	//            <EndTime>17:30</EndTime>
	//            <AvailableNumStr>173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200</AvailableNumStr>
	//            <AvailableTotalNum>28</AvailableTotalNum>
	//            <AvailableLeftNum>28</AvailableLeftNum>
	//        </TimeRange>
	//    </TimeRanges>
	//</Response>
	//`

	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), &resp10041)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp10041 failed ", err)
		return nil, err
	}

	if resp10041.ResultCode != "0" {
		return nil, errors.New(resp10041.ResultContent)
	}
	if resp10041.ResultContent == "号已挂完" {
		return nil, errors.New(resp10041.ResultContent)
	}

	return resp10041.TimeRanges, nil
}

func (rg *HisService) LockOrder(req *models.Req10015Params) (*models.Resp10015, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.LockOrder).
		Xml("TradeCode", TradeCode.LockOrder).
		Xml("PatientCard", "").
		Xml("PatientID", req.PatientId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("ScheduleItemCode", req.ScheduleItemCode).
		Xml("PayOrderId", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("TradeDate", req.TradeDate).
		Xml("TradeTime", req.TradeTime).
		//Xml("TradeDate", time.Now().Add(8*time.Hour).Format("2006-01-02")).
		//Xml("TradeTime", time.Now().Add(8*time.Hour).Format("15:04:05")).
		Xml("BeginTime", req.StartTime).
		Xml("EndTime", req.EndTime).
		Xml("TradeDate", "").
		Xml("TradeTime", "").
		Xml("BillTypeID", "").
		Xml("CardNo", "").
		Xml("CardType", "").
		Xml("CredTypeCode", "").
		Xml("IDCardNo", "").
		Xml("TransactionId", "").
		Xml("LockQueueNo", req.LockQueueNo).
		Xml("Mobile", "").
		Xml("PayOrdId", "").
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp10015 := &models.Resp10015{}

	//  data := `<Response>
	//    <TradeCode>10015</TradeCode>
	//    <TransactionId>DHC000169200728174747</TransactionId>
	//    <ResultCode>0</ResultCode>
	//    <ResultContent>加号成功</ResultContent>
	//    <LockQueueNo>1</LockQueueNo>
	//    <ScheduleItemCode>1338||3725</ScheduleItemCode>
	//    <AdmDoc>主治医师</AdmDoc>
	//    <AdmDate>2020-07-28</AdmDate>
	//    <AdmTime>下午</AdmTime>
	//    <RegFee>11</RegFee>
	//</Response>
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), &resp10015)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp10015 failed ", err)
		return nil, err
	}

	if resp10015.ResultCode != "0" {
		return nil, errors.New(resp10015.ResultContent)
	}

	return resp10015, nil
}

func (rg *HisService) UnLockOrder(req *models.Req10016Params) (*models.Resp10016, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.UnLockOrder).
		Xml("TradeCode", TradeCode.UnLockOrder).
		Xml("HospitalId", constant.HospitalId).
		Xml("ScheduleItemCode", req.ScheduleItemCode).
		Xml("TransactionId", req.TransactionId).
		Xml("LockQueueNo", req.LockQueueNo).
		Xml("ExtUserID", constant.ExtUserID).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp10016 := &models.Resp10016{}

	//  data := `<Response>
	//    <TradeCode>10016</TradeCode>
	//    <ResultCode>0</ResultCode>
	//    <ResultContent>取消订单成功</ResultContent>
	//</Response>
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), &resp10016)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp10016 failed ", err)
		return nil, err
	}

	if resp10016.ResultCode != "0" {
		return nil, errors.New(resp10016.ResultContent)
	}

	return resp10016, nil
}

func (rg *HisService) Register(req *models.Req1101Params) (*models.Resp1101, error) {
	if req.PayModeCode == "" {
		req.PayModeCode = constant.PayModeCode
	}
	var requestXml, payDetailsXml helper.XmlString
	_ = payDetailsXml.Xml("PayModeCode", req.PayModeCode).
		Xml("PayAmt", req.PayAmt).
		Xml("PlatformNo", req.PlatformNo).
		Xml("OutPayNo", req.OutPayNo).
		Xml("PayChannel", req.PayChannel).
		Xml("POSPayStr", req.POSPayStr).
		Xml("PayDate", req.PayDate).
		Xml("PayTime", req.PayTime)
	_ = *requestXml.BeginXml(MesCode.Register).
		Xml("TradeCode", TradeCode.Register).
		Xml("TransactionId", req.TransactionId).
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("TerminalID", "").
		Xml("ScheduleItemCode", req.ScheduleItemCode).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("PatientCard", "").
		Xml("CardType", "").
		Xml("PatientID", req.PatientID).
		Xml("BillTypeID", "").
		Xml("PayBankCode", "").
		Xml("PayCardNo", "").
		Xml("PayModeCode", req.PayModeCode).
		Xml("PayFee", req.PayFee).
		Xml("PayInsuFeeStr", "").
		Xml("PayTradeNo", "").
		Xml("QueueNo", "").
		Xml("AppOrderCode", req.AppOrderCode).
		Xml("PayDetails", string(payDetailsXml)).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp1101 := &models.Resp1101{}

	//  data := `<Response>
	//	<ResultCode>435</ResultCode>
	//	<ResultContent>5345</ResultContent>
	//	<SeqCode>3453</SeqCode>
	//	<RegFee>345</RegFee>
	//	<AdmitRange>4353</AdmitRange>
	//	<AdmitAddress>erts</AdmitAddress>
	//	<TransactionId>ewrt</TransactionId>
	//	<PayOrderId>etrw</PayOrderId>
	//	<AdmNo>terw</AdmNo>
	//	<DeptName>wetr</DeptName>
	//	<DoctorName>wte</DoctorName>
	//	<DoctorLevelDesc>wet</DoctorLevelDesc>
	//	<DoctorLevelCode>wttre</DoctorLevelCode>
	//	<ReceiptNo>et</ReceiptNo>
	//	<TimeRange>wet</TimeRange>
	//</Response>
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), &resp1101)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1101 failed ", err)
		return nil, err
	}

	if resp1101.ResultCode != "0" {
		return resp1101, errors.New(resp1101.ResultContent)
	}

	return resp1101, nil
}

func (rg *HisService) QueryAdmOPReg(req *models.Req1104Params) (*models.Resp1104, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.QueryAdmOPReg).
		Xml("TradeCode", TradeCode.QueryAdmOPReg).
		Xml("HospitalId", constant.HospitalId).
		//Xml("ExtUserID", constant.ExtUserID).
		Xml("ExtUserID", "").
		Xml("PatientID", req.PatientID).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		EndXml()

	result, err := helper.Post(string(requestXml))
	middlewares.Logger.Println("QueryAdmOPReg resp1104 ", result)
	if err != nil {
		return nil, err
	}

	resp1104 := &models.Resp1104{}

	//data := `<Response><ResultCode>0</ResultCode><ResultContent>查询成功</ResultContent><RecordCount>3</RecordCount><Orders><Order><AdmNo>2035245</AdmNo><RegDate>2020-07-17</RegDate><Status>正常</Status><PatName>谢平</PatName><PatientID>0000000169</PatientID><AdmitDate>2020-07-17</AdmitDate><HospitalName>成都市公共卫生临床医疗中心</HospitalName><DepartmentCode>119</DepartmentCode><Department>急诊医学科（航天）</Department><DoctorCode>179</DoctorCode><Doctor>石雪梅</Doctor><DoctorTitle>急诊</DoctorTitle><RegFee> 0.00元</RegFee><SessionName>全天</SessionName><AdmitRange>0000000169</AdmitRange><ReturnFlag>N</ReturnFlag></Order><Order><AdmNo>2035246</AdmNo><RegDate>2020-07-22</RegDate><Status>正常</Status><PatName>谢平</PatName><PatientID>0000000169</PatientID><AdmitDate>2020-07-22</AdmitDate><HospitalName>成都市公共卫生临床医疗中心</HospitalName><DepartmentCode>284</DepartmentCode><Department>普通传染门诊</Department><DoctorCode>179</DoctorCode><Doctor>石雪梅</Doctor><DoctorTitle>主治医师</DoctorTitle><RegFee> 0.00元</RegFee><SessionName>下午</SessionName><AdmitRange>0000000169</AdmitRange><ReturnFlag>N</ReturnFlag></Order><Order><AdmNo>2035247</AdmNo><RegDate>2020-07-23</RegDate><Status>正常</Status><PatName>谢平</PatName><PatientID>0000000169</PatientID><AdmitDate>2020-07-23</AdmitDate><HospitalName>成都市公共卫生临床医疗中心</HospitalName><DepartmentCode>284</DepartmentCode><Department>普通传染门诊</Department><DoctorCode>179</DoctorCode><Doctor>石雪梅</Doctor><DoctorTitle>主治医师</DoctorTitle><RegFee> 0.00元</RegFee><SessionName>下午</SessionName><AdmitRange>0000000169</AdmitRange><ReturnFlag>N</ReturnFlag></Order></Orders></Response>
	//`
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp1104)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1104 failed ", err)
		return nil, err
	}

	middlewares.Logger.Println("QueryAdmOPReg resp1104 ", resp1104)

	if resp1104.ResultCode != "0" {
		return nil, errors.New(resp1104.ResultContent)
	}

	return resp1104, nil
}

func (rg *HisService) OPRegReturn(req *models.Req1003Params) (*models.Resp1003, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml(MesCode.OPRegReturn).
		Xml("TradeCode", TradeCode.OPRegReturn).
		Xml("HospitalId", constant.HospitalId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("TransactionId", req.TransactionId).
		Xml("AdmNo", req.AdmNo).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp1003 := &models.Resp1003{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp1003)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1003 failed ", err)
		return nil, err
	}

	if resp1003.ResultCode != "0" {
		return nil, errors.New(resp1003.ResultContent)
	}
	return resp1003, nil
}

func (rg *HisService) GetOpReport(req *models.ReqReport) (*models.RespReport, error) {
	var requestXml helper.XmlString
	_ = *requestXml.ReportBeginXml(MesCode.GetOPReport).
		Xml("PATPatientID", req.PatientId).
		//Xml("PATPatientID", req.PatientId).
		Xml("PAADMVisitNumber", "").
		Xml("DocumentType", "").
		Xml("StartDate", req.Start).
		Xml("EndDate", req.End).
		Xml("DocumentFormat", "xml").
		ReportEndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	respReport := &models.RespReport{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), respReport)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1003 failed ", err)
		return nil, err
	}

	if respReport.Body.ResultCode != "0" {
		return nil, errors.New(respReport.Body.ResultContent)
	}

	return respReport, nil
}

// 退费回写标记
func (rg *HisService) SaveSelfRefundInfo(req *models.RegisterModel) (*models.RespReport, error) {
	var requestXml, payDetailsXml helper.XmlString
	_ = payDetailsXml.Xml("PayModeCode", constant.PayModeCode).
		Xml("TradeChannel", constant.PayModeCode).
		Xml("PayAmt", req.Fee)
	_ = *requestXml.BeginXml("MES0034").
		Xml("CardNo", "").
		Xml("CardType", "").
		Xml("SecrityNo	", "").
		Xml("PatientID", req.PatientId).
		Xml("UserCode", constant.UserCode).
		Xml("TerminalID", "").
		Xml("HospitalID", "2").
		Xml("InvoiceNoStr", req.InvoiceStr).
		Xml("OrderNo", req.OrderSn).
		Xml("PayDetails", string(payDetailsXml)).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	respReport := &models.RespReport{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), respReport)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp1003 failed ", err)
		return nil, err
	}

	if respReport.Body.ResultCode != "0" {
		return nil, errors.New(respReport.Body.ResultContent)
	}

	return respReport, nil
}

//
///*
//*获取有效就诊记录（5001）
// */
//func (rg *HisService) GetIPAdmInfo(req *models.Req5001Params) (*models.Resp5001, error) {
//	var requestXml helper.XmlString
//	_ = *requestXml.BeginXml("MES0053").
//		Xml("TradeCode", "5001").
//		Xml("HospitalID", "2").
//		Xml("CardNo", "").
//		Xml("SecrityNo", "").
//		Xml("CardType", "").
//		Xml("PatientID", req.PatientID).
//		Xml("MedicalNo", "").
//		Xml("UserCode", constant.UserCode).
//		Xml("TerminalID", "").
//		Xml("AimFlag", req.AimFlag).
//		Xml("StartDate", req.StartDate).
//		Xml("EndDate", req.EndDate).
//		Xml("ExpStr", "").
//		EndXml()
//	middlewares.Logger.Println("GetIPAdmInfo xml ", requestXml)
//	result, err := helper.Post(string(requestXml))
//	if err != nil {
//		return nil, err
//	}
//	middlewares.Logger.Println("GetIPAdmInfo result ", result.Body.MiResp.MiResult)
//	data := &models.Resp5001{}
//	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
//	if err != nil {
//		middlewares.Logger.Println("Unmarshal resp5001 failed ", err)
//		return nil, err
//	}
//	//data.Req = string(requestXml)
//	//data.Resp = result.Body.MiResp.MiResult
//	return data, nil
//}
////
///*
//*住院押金充值（5002）
// */
//func (op *HisService) AddIPDeposit(req *models.Req5002Params) (*models.Resp5002, error) {
//	var requestXml, payDetailsXml helper.XmlString
//	_ = payDetailsXml.Xml("PayModeCode", constant.PayModeCode).
//		Xml("TradeChannel", req.PayDetails.TradeChannel).
//		Xml("PayAccountNo", req.PayDetails.PayAccountNo).
//		Xml("PayAmt", req.PayDetails.PayAmt).
//		Xml("PlatformNo", req.PayDetails.PlatformNo).
//		Xml("OutPayNo", req.PayDetails.OutPayNo).
//		Xml("PayChannel", req.PayDetails.PayChannel).
//		Xml("POSPayStr", req.PayDetails.POSPayStr).
//		Xml("PayDate", req.PayDetails.PayDate).
//		Xml("PayTime", req.PayDetails.PayTime)
//
//	_ = *requestXml.BeginXml("MES0054").
//		Xml("TradeCode", req.TradeCode).
//		Xml("HospitalID", req.HospitalId).
//		Xml("CardNo", req.CardNo).
//		Xml("CardType", req.CardType).
//		Xml("SecrityNo", req.SecrityNo).
//		Xml("PatientID", req.PatientID).
//		Xml("UserCode", req.UserCode).
//		Xml("TerminalID", req.TerminalID).
//		Xml("AdmID", req.AdmID).
//		Xml("PayDetails", string(payDetailsXml)).
//		EndXml()
//	middlewares.Logger.Println("AddIPDeposit xml ", requestXml)
//	result, err := helper.PostTestHis(string(requestXml))
//	if err != nil {
//		return nil, err
//	}
//	middlewares.Logger.Println("AddIPDeposit result ", result.Body.MiResp.MiResult)
//	data := &models.Resp5002{}
//	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
//	if err != nil {
//		middlewares.Logger.Println("Unmarshal resp5002 failed ", err)
//		return nil, err
//	}
//	//data.Req = string(requestXml)
//	//data.Resp = result.Body.MiResp.MiResult
//	return data, nil
//}
//
///*
//*押金记录查询（5003）
// */
//func (op *HisService) GetIPDepRecord(req *models.Req5003Params) (*models.Resp5003, error) {
//	var requestXml helper.XmlString
//	_ = *requestXml.BeginXml("MES0055").
//		Xml("TradeCode", req.TradeCode).
//		Xml("HospitalID", req.HospitalId).
//		Xml("CardNo", req.CardNo).
//		Xml("SecrityNo", req.SecrityNo).
//		Xml("CardType", req.CardType).
//		Xml("PatientID", req.PatientID).
//		Xml("MedicalNo", req.MedicalNo).
//		Xml("UserCode", req.UserCode).
//		Xml("TerminalID", req.TerminalID).
//		Xml("AdmID", req.AdmID).
//		Xml("ExpStr", req.ExpStr).
//		EndXml()
//	middlewares.Logger.Println("GetIPDepRecord xml ", requestXml)
//	result, err := helper.PostTestHis(string(requestXml))
//	if err != nil {
//		return nil, err
//	}
//	middlewares.Logger.Println("GetIPDepRecord result ", result.Body.MiResp.MiResult)
//	data := &models.Resp5003{}
//	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
//	if err != nil {
//		middlewares.Logger.Println("Unmarshal resp5003 failed ", err)
//		return nil, err
//	}
//	data.Req = string(requestXml)
//	data.Resp = result.Body.MiResp.MiResult
//	return data, nil
//}

/*
*获取住院证信息（5010）
 */
func (op *HisService) GetIPRegInfo(req *models.Req5010Params) (*models.Resp5010, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml("MES0059").
		Xml("TradeCode", "5010").
		Xml("HospitalID", "2").
		Xml("CardNo", req.CardNo).
		Xml("SecrityNo", req.SecrityNo).
		Xml("CardType", req.CardType).
		Xml("PatientID", req.PatientID).
		Xml("MedicalNo", req.MedicalNo).
		Xml("UserCode", "Wechat").
		Xml("TerminalID", req.TerminalID).
		Xml("ExpStr", req.ExpStr).
		Xml("IDNo", req.IDNo).
		Xml("IDType", req.IDType).
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	data := &models.Resp5010{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp5010 failed ", err)
		return nil, err
	}
	return data, nil
}

/*
*住院登记（5011）
 */
func (op *HisService) IPRegister(req *models.Req5011Params) (*models.Resp5011, error) {
	var requestXml, admInfoXml helper.XmlString
	_ = admInfoXml.Xml("AdmReason", req.AdmInfo.AdmReason)
	_ = *requestXml.BeginXml("MES0060").
		Xml("TradeCode", "5011").
		Xml("HospitalID", "2").
		Xml("CardNo", req.CardNo).
		Xml("UserCode", constant.UserCode).
		Xml("CardType", req.CardType).
		Xml("SecrityNo", req.SecrityNo).
		Xml("PatientID", req.PatientID).
		Xml("MedicalNo", req.MedicalNo).
		Xml("IPBook", req.IPBook).
		Xml("PatInfo", req.PatInfo).
		Xml("PayDetails", string(admInfoXml)).
		Xml("ExpStr", req.ExpStr).
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	data := &models.Resp5011{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp5010 failed ", err)
		return nil, err
	}
	return data, nil
}

func (op *HisService) GetIPAdmInfo(req *models.Req5001Params) (*models.Resp5001, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml("MES0021").
		Xml("TradeCode", "5001").
		Xml("HospitalID", "2").
		Xml("CardNo", req.CardNo).
		Xml("SecrityNo", "").
		Xml("CardType", "").
		Xml("PatientID", req.PatientID).
		Xml("MedicalNo", "").
		Xml("UserCode", constant.UserCode).
		Xml("TerminalID", "").
		Xml("AimFlag", req.AimFlag).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("ExpStr", "").
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	data := &models.Resp5001{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp5001 failed ", err)
		return nil, err
	}
	//data.Req = string(requestXml)
	//data.Resp = result.Body.MiResp.MiResult
	return data, nil
}

func (op *HisService) AddIPDeposit(req *models.ReqIpDeposit) (*models.Resp5002, error) {
	var requestXml, payDetailsXml helper.XmlString
	_ = payDetailsXml.Xml("PayModeCode", "ZZJZF").
		Xml("TradeChannel", "ZZJ").
		Xml("PayAmt", strconv.FormatFloat(req.Fee, 'f', 2, 64)).
		Xml("PlatformNo", req.OrderSn).
		Xml("OutPayNo", req.OrderSn).
		Xml("PayChannel", "ZZJ").
		Xml("POSPayStr", "").
		Xml("PayDate", time.Now().Format("2006-01-02")).
		Xml("PayTime", time.Now().Format("15:03:04"))
	_ = *requestXml.BeginXml("MES0022").
		Xml("TradeCode", "5002").
		Xml("HospitalID", "2").
		Xml("CardNo", "").
		Xml("AdmID", req.AdmID).
		Xml("PatientID", req.PatientID).
		Xml("UserCode", constant.UserCode).
		Xml("PayDetails", string(payDetailsXml)).
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	data := &models.Resp5002{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp5002 failed ", err)
		return nil, err
	}
	//data.Req = string(requestXml)
	//data.Resp = result.Body.MiResp.MiResult
	return data, nil
}

func (op *HisService) GetIPDepRecord(req *models.ReqIpDepositRecord) (*models.Resp5003, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml("MES0023").
		Xml("TradeCode", "5003").
		Xml("HospitalID", "2").
		Xml("CardNo", "").
		Xml("SecrityNo", "").
		Xml("CardType", "").
		Xml("PatientID", req.PatientID).
		Xml("MedicalNo", "").
		Xml("AdmID", req.AdmID).
		Xml("TerminalID", "").
		Xml("UserCode", constant.UserCode).
		Xml("ExpStr", "").
		EndXml()
	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}
	data := &models.Resp5003{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), data)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp5003 failed ", err)
		return nil, err
	}
	//data.Req = string(requestXml)
	//data.Resp = result.Body.MiResp.MiResult
	return data, nil
}

//
//func (op *HisService) GetFeeList(req *models.Req11003Params) (*models.Resp11003, error) {
//	var requestXml helper.XmlString
//	_ = *requestXml.FeeListBeginXml("11003").
//		Xml("CardNo", req.CardNo).
//		Xml("CardType", req.CardType).
//		Xml("CardTypeCode", "").
//		Xml("StartDate", req.StartDate).
//		Xml("EndDate", req.EndDate).
//		Xml("CurrentIH", req.CurrentIH).
//		Xml("AdmType", req.AdmType).
//		Xml("IsApp", "").
//		Xml("AppMethodCode", req.AppMethodCode).
//		Xml("HospCode", "").
//		Xml("LocCode", req.LocCode).
//		Xml("DocCode", "").
//		Xml("WardCode", "").
//		Xml("ClientType", "").
//		Xml("Count", req.Count).
//		Xml("Page", req.Page).
//		Xml("PatientName", "").
//		Xml("RowId", "").
//		FeeListEndXml()
//
//	middlewares.Logger.Println("get IP Fee List xml ", requestXml)
//	data, err := helper.PostHisFeeList(string(requestXml))
//	//middlewares.Logger.Println("get IP Fee List result ==============================", result)
//	if err != nil {
//		return nil, err
//	}
//
//	//data := &models.Resp11003{}
//	//err = xml.Unmarshal([]byte(result.Response), data)
//	//if err != nil {
//	//	middlewares.Logger.Println("Unmarshal Resp11003 failed ", err)
//	//	return nil, err
//	//}
//	//data.Req = string(requestXml)
//	//data.Resp = result.BodyFee.MiRespFee.MiResultFee
//
//	if err != nil {
//		middlewares.Logger.Println("read response body failed", err)
//		return nil, err
//	}
//	result := &models.Resp11003{}
//	err = xml.Unmarshal(data, result)
//	if err != nil {
//		middlewares.Logger.Println("Unmarshal response body failed", err)
//		return nil, err
//	}
//
//	return result, nil
//}

func (op *HisService) InpatientInfo(req *models.Req11003Params) (*models.Resp11003, error) {
	var requestXml helper.XmlString
	_ = *requestXml.FeeListBeginXml("11003").
		Xml("CardNo", req.CardNo).
		Xml("CardType", req.CardType).
		Xml("CardTypeCode", "").
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("CurrentIH", req.CurrentIH).
		Xml("AdmType", req.AdmType).
		Xml("IsApp", "").
		Xml("AppMethodCode", req.AppMethodCode).
		Xml("HospCode", "").
		Xml("LocCode", req.LocCode).
		Xml("DocCode", "").
		Xml("WardCode", "").
		Xml("ClientType", "").
		Xml("Count", req.Count).
		Xml("Page", req.Page).
		Xml("PatientName", "").
		Xml("RowId", "").
		FeeListEndXml()
	middlewares.Logger.Println("1===========================", string(requestXml))
	data, err := helper.PostHisFeeList(string(requestXml))
	middlewares.Logger.Println("1===========================", data)
	if err != nil {
		middlewares.Logger.Println("read response body failed", err)
		return nil, err
	}
	result := &models.Resp11003{}
	err = xml.Unmarshal([]byte(data.Body.MiResp.MiResultFee), result)
	if err != nil {
		middlewares.Logger.Println("Unmarshal response body failed", err)
		return nil, err
	}

	return result, nil
}

func (op *HisService) OperationInfo(req *models.ReqOpera) (*models.RespOperation, error) {
	var requestXml helper.XmlString
	_ = *requestXml.OperaBeginXml("SendUserDataInfo").
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		OperaEndXml()

	data, err := helper.PostHisV3(string(requestXml))
	if err != nil {
		return nil, err
	}
	middlewares.Logger.Println("xml", data)
	if err != nil {
		middlewares.Logger.Println("read response body failed", err)
		return nil, err
	}
	result := &models.RespOperation{}
	err = xml.Unmarshal([]byte(data.Body.MiResp.MiResultFee), result)
	if err != nil {
		middlewares.Logger.Println("Unmarshal response body failed", err)
		return nil, err
	}

	return result, nil
}

func (rg *HisService) QueryOrder(req *models.Req1005Params) (*models.Resp1005, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml("MES0030").
		Xml("TradeCode", "1005").
		Xml("ExtOrgCode", "").
		Xml("ClientType", "").
		Xml("HospitalId", constant.HospitalId).
		Xml("ExtUserID", constant.ExtUserID).
		Xml("CardNo", "").
		Xml("CredTypeCode", "").
		Xml("IDCardNo", "").
		Xml("PatientNo", req.PatientNo).
		Xml("OrderApptStartDate", req.OrderApptStartDate).
		Xml("OrderApptEndDate", req.OrderApptEndDate).
		Xml("QueryDateFlag", req.QueryDateFlag).
		Xml("QueryUserType", "").
		Xml("OrderStatus", "").
		Xml("OrderCode", "").
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	Resp1005 := &models.Resp1005{}

	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), Resp1005)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp10041 failed ", err)
		return nil, err
	}

	if Resp1005.ResultCode != "0" {
		return nil, errors.New(Resp1005.ResultContent)
	}
	return Resp1005, nil
}

func (op *HisService) GetFeeList(req *models.Req11003Params) (*models.Resp11003, error) {
	var requestXml helper.XmlString
	_ = *requestXml.FeeListBeginXml("11003").
		Xml("CardNo", req.CardNo).
		Xml("CardType", req.CardType).
		Xml("CardTypeCode", "").
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("CurrentIH", req.CurrentIH).
		Xml("AdmType", req.AdmType).
		Xml("IsApp", "").
		Xml("AppMethodCode", req.AppMethodCode).
		Xml("HospCode", "").
		Xml("LocCode", req.LocCode).
		Xml("DocCode", "").
		Xml("WardCode", "").
		Xml("ClientType", "").
		Xml("Count", req.Count).
		Xml("Page", req.Page).
		Xml("PatientName", "").
		Xml("RowId", "").
		FeeListEndXml()

	data, err := helper.PostHisFeeList(string(requestXml))
	if err != nil {
		return nil, err
	}
	if err != nil {
		middlewares.Logger.Println("read response body failed", err)
		return nil, err
	}
	result := &models.Resp11003{}
	err = xml.Unmarshal([]byte(data.Body.MiResp.MiResultFee), result)
	if err != nil {
		middlewares.Logger.Println("Unmarshal response body failed", err)
		return nil, err
	}
	return result, nil
}

func (op *HisService) GetFeeDaily(req *models.ReqFeeDaily) (*models.RespFeeDaily, error) {
	var requestXml helper.XmlString
	_ = *requestXml.BeginXml("MES0056").
		//Xml("EpisodeNo", req.EpisodeNo).
		//Xml("ClientType", req.ClientType).
		//Xml("StartDate", req.StartDate).
		//Xml("EndDate", req.EndDate).
		//Xml("OrderCat", req.OrderCat).
		//Xml("OEORDLoc", req.OEORDLoc).
		//Xml("OEORDDoc", req.OEORDDoc).
		Xml("TradeCode", req.TradeCode).
		Xml("AdmID", req.AdmID).
		Xml("BillDate", req.BillDate).
		EndXml()

	result, err := helper.Post(string(requestXml))
	if err != nil {
		return nil, err
	}

	resp21001 := &models.RespFeeDaily{}
	err = xml.Unmarshal([]byte(result.Body.MiResp.MiResult), resp21001)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp21001 failed ", err)
		return nil, err
	}
	//data.Req = string(requestXml)
	//data.Resp = result.BodyFee.MiRespFee.MiResultFee
	return resp21001, nil
}

func (op *HisService) GetFeeListDetail(req *models.Req21001Params) (*models.Resp21001, error) {
	var requestXml helper.XmlString
	_ = *requestXml.FeeListDetailBeginXml("21001").
		Xml("EpisodeID", req.EpisodeNo).
		Xml("ClientType", req.ClientType).
		Xml("StartDate", req.StartDate).
		Xml("EndDate", req.EndDate).
		Xml("OrderCat", req.OrderCat).
		Xml("OEORDLoc", req.OEORDLoc).
		Xml("OEORDDoc", req.OEORDDoc).
		FeeListDetailEndXml()

	data, err := helper.PostHisFeeList(string(requestXml))
	if err != nil {
		return nil, err
	}

	result := &models.Resp21001{}
	err = xml.Unmarshal([]byte(data.Body.MiResp.MiResultFee), result)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp21001 failed ", err)
		return nil, err
	}
	//data.Req = string(requestXml)
	//data.Resp = result.BodyFee.MiRespFee.MiResultFee
	return result, nil
}

// 免挂号就诊
func (rg *HisService) Mghjz32004(req *models.Req32004) ([]byte, error) {
	_, filename, _, _ := runtime.Caller(0)
	path := strings.Split(filename, "/")
	env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-3], "/"))
	dotenv.Load(env)
	result, err := helper.PostJsonRes(cmn.GetEnv("HIS_URL_REST")+"?apiCode=32004", req)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// 保存医嘱/处方
func (op *HisService) Bcyz32010(req *models.Req32010) ([]byte, error) {
	_, filename, _, _ := runtime.Caller(0)
	path := strings.Split(filename, "/")
	env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-3], "/"))
	dotenv.Load(env)
	result, err := helper.PostJsonRes(cmn.GetEnv("HIS_URL_REST")+"?apiCode=32010", req)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// 绑定银行卡
func (op *HisService) BindBankCard(req *models.ReqBindBank) ([]byte, error) {
	_, filename, _, _ := runtime.Caller(0)
	path := strings.Split(filename, "/")
	env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-3], "/"))
	dotenv.Load(env)
	result, err := helper.PostJsonRes(cmn.GetEnv("HIS_URL_REST")+"?apiCode=12016", req)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// 查询银行卡
func (op *HisService) QueryBankCard(req *models.ReqQueryBank) (*models.RespQueryBank, error) {
	_, filename, _, _ := runtime.Caller(0)
	path := strings.Split(filename, "/")
	env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-3], "/"))
	dotenv.Load(env)
	result, err := helper.PostJsonRes(cmn.GetEnv("HIS_URL_REST")+"?apiCode=11013", req)
	if err != nil {
		middlewares.Logger.Println("post json QueryBankCard failed ", err)
		return nil, err
	}
	//bytes, err := json.Marshal(result)
	//middlewares.Logger.Println("=====================2", bytes)
	d := &models.RespQueryBank{}
	err = json.Unmarshal(result, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal RespQueryBank failed ", err)
		return nil, err
	}
	return d, nil
}

// 解绑银行卡
func (op *HisService) UnbindBankCard(req *models.ReqUnBindBank) ([]byte, error) {
	_, filename, _, _ := runtime.Caller(0)
	path := strings.Split(filename, "/")
	env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-3], "/"))
	dotenv.Load(env)
	result, err := helper.PostJsonRes(cmn.GetEnv("HIS_URL_REST")+"?apiCode=12017", req)
	if err != nil {
		return nil, err
	}
	return result, nil
}
